package com.dragon.modules.yuegou.service.impl.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.modules.yuegou.entity.coupon.CouponGiftEntity;
import com.dragon.modules.yuegou.entity.member.DistrictMemberEntity;
import com.dragon.modules.yuegou.enums.coupon.CouponGiftType;
import com.dragon.modules.yuegou.mapper.coupon.CouponGiftMapper;
import com.dragon.modules.yuegou.model.coupon.CouponGift;
import com.dragon.modules.yuegou.query.coupon.CouponGiftQuery;
import com.dragon.modules.yuegou.query.member.DistrictMemberQuery;
import com.dragon.modules.yuegou.service.coupon.CouponGiftService;
import com.dragon.modules.yuegou.service.coupon.CouponService;
import com.dragon.modules.yuegou.service.member.DistrictMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;


/**
 * 停车券赠送任务信息表 服务实现
 *
 * @author pbuilder generated
 * @date 2019-10-25 15:45:17
 * @version 2.0
 */
@Service("coupCouponGiftService")
public class CouponGiftServiceImpl extends ServiceImpl<CouponGiftMapper, CouponGiftEntity>
		implements CouponGiftService {

	@Autowired
	private DistrictMemberService districtMemberService;

	@Autowired
	private CouponService couponService;
	
	@Override
	public void sendGift(CouponGift couponGift) {
		long pageNum = 1;
		while (pageNum > 0) {
			DistrictMemberQuery districtMemberQuery = new DistrictMemberQuery();
			districtMemberQuery.setBusdId(couponGift.getBusdId());
			IPage<DistrictMemberEntity> page = new Page<>(pageNum,10000L,false);
			QueryWrapper<DistrictMemberEntity> wrapper = districtMemberQuery.toQueryWrapper();
			wrapper.orderByAsc(DistrictMemberEntity.COL_ID);
			Set<String> memberIdList = districtMemberService.page(page,wrapper).getRecords()
					.stream().map(DistrictMemberEntity::getMemberId).collect(Collectors.toSet());
			pageNum++;
			if (memberIdList.size() < 10000) {
				pageNum = 0;
			}
			for (int i = 0; i < couponGift.getSendNum(); i++) {
				couponService.bachCreateCoupon(memberIdList, couponGift.getTemplateId(), "发券任务");
			}
		}
	}

	@Override
	public List<CouponGift> findGiftList(String busdId) {
		LocalDateTime now = LocalDateTime.now();
		CouponGiftQuery query = new CouponGiftQuery();
		query.busdId().eq(busdId);
		query.setDel(false);
		query.setStatus(true);
		query.sendNum().gt(0);
		query.beginTime().lt(now);
		query.endTime().gt(now);

		List<CouponGiftEntity> list = this.list(query.toQueryWrapper());
		return list.stream().map(this::checkRight).filter(Objects::nonNull).collect(Collectors.toList());
	}

	@Override
	public void sendMemberGift(String busdId, String memberId) {
		if (StringUtils.isNotEmpty(busdId) && StringUtils.isNotEmpty(memberId)){
			Set<String> memberIdList = new HashSet<>();
			memberIdList.add(memberId);
			List<CouponGift> couponGiftList = findGiftList(busdId);
			couponGiftList.stream().forEach(couponGift ->couponService.bachCreateCoupon(memberIdList, couponGift.getTemplateId(), "注册发券任务"));
		}
	}

	private CouponGift checkRight(CouponGiftEntity couponGift) {
		if (couponGift.getType() == CouponGiftType.DAY) {
			return new CouponGift(couponGift);
		}
		if (couponGift.getType() == CouponGiftType.WEEK && couponGift.getCondition() == LocalDateTime.now().getDayOfWeek().getValue()) {
			return new CouponGift(couponGift);
		}
		if (couponGift.getType() == CouponGiftType.MONTH && couponGift.getCondition() == LocalDateTime.now().getDayOfMonth()) {
			return new CouponGift(couponGift);
		}
		return null;
	}
}